<?php

/**
 * 自动生成 MerContentController
 * 
 * Apps/Backend/Controller/MerContentController.class.php
 * admin @ 2015-06-12
 */

namespace Backend\Controller;

use Common\Service\MerchantService ;
use Common\Service\MerContentService;
use Common\Service\MerDictionaryService;

class MerContentController extends BackendController {

  public function index( $mer_id = 0 ) {
    $action = I('get.action', 'read');

    $data = $this->_init_data();
    $data['module_name'] = '内容';
    $data['module_url'] = U( $this->base_path . 'index') .'/' . $mer_id ;

    $data['catalog_name'] = '商户';
    $data['catalog_url'] = U( $this->base_path . 'index') .'/' . $mer_id;
    
    $Merchant = MerchantService::instance() ;
    $data['merchant_data'] = $Merchant->get_by_id( $mer_id );
    if ( empty( $data['merchant_data'] ) ) {
      gameover('商户不存在');
    }
    
    $MerContent = MerContentService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => 'Merchant_Menu',
      'mer_id' => $mer_id ,
      'module_url' => U( $this->base_path . 'index' ) . '/' . $mer_id ,
      'read_url'   => U( $this->base_path . 'read'  ),
      'create_url' => U( $this->base_path . 'create'),
      'update_url' => U( $this->base_path . 'update'),
      'delete_url' => U( $this->base_path . 'delete'),
      'upload_icon_url' => U(  $this->base_path . 'upload') . '/icon' ,
      'del_icon_url' => U($this->base_path . 'delete_icon'),
      'upload_ke_img_url' => U($this->base_path . 'upload') .'/kindeditor',
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $MerContent->get_default_row() 
    );

    $MerDictionary = MerDictionaryService::instance();
    $data['param'] = array(
      'catalog' => $MerDictionary->get_content_catalog_by_merchant($mer_id) ,
      'status' => array( 0 => '禁用' , 1 => '启用'),
    );

    $this->display_with_layout( $data , $this->controller . ':index');
  }

  function read() {
    $post = I('post.');

    $config = array(
      'mer_id' => $post['mer_id'],
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'mer_id' => $post['mer_id'] ,
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $MerContent = MerContentService::instance();
    $data['rows'] = $MerContent->get_by_cond($config);

    $config['count'] = TRUE;
    $data['total'] = $MerContent->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $MerContent = MerContentService::instance();
    $ret = $MerContent->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $post['update_date'] = date('Y-m-d H:i:s');
    $MerContent = MerContentService::instance();
    $ret = $MerContent->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $MerContent = MerContentService::instance();
    $ret = $MerContent->delete($ids); 
    $this->ajaxReturn($ret);
  }
  

}
